using System;
using System.Data;

namespace System.Resources.SqlResource
{
    /// <summary>
    /// Collection of resources.
    /// </summary>
    internal sealed class CSResources : Collections.IDictionaryEnumerator
    {
        private int _pos = -1;
        private DataTable _dt = null;

        public CSResources(DataTable dt)
        {
            _pos = -1;
            _dt = dt;
        }

        #region IEnumerator Members
        public object Current
        {
            get {return Entry;}
        }

        public bool MoveNext()
        {
            if (_pos == _dt.Rows.Count-1) {return false;}
            
            _pos++;
            return true;
        }

        public void Reset()
            {_pos = -1;}
        #endregion

        #region IDictionaryEnumerator Members
        public Collections.DictionaryEntry Entry
        {
            get 
            {
                Collections.DictionaryEntry de = new Collections.DictionaryEntry();
                CSResource res = new CSResource();
                res.ParamName = _dt.Rows[_pos]["ParamName"].ToString();
                res.ParamType = _dt.Rows[_pos]["ParamType"].ToString();

                if (_dt.Rows[_pos]["ParamStringValue"] != DBNull.Value)
                    {res.ParamStringValue = _dt.Rows[_pos]["ParamStringValue"].ToString();}
                else
                    {res.ParamStringValue = null;}

                if (_dt.Rows[_pos]["ParamObjectValue"] != DBNull.Value)
                {
                    //res.ParamObjectValue = (byte[])_dt.Rows[_pos]["ParamObjectValue"];
                    res.ParamObjectValue = ObjectByteArrayConverter.ByteArrayToObject( (byte[])_dt.Rows[_pos]["ParamObjectValue"]);
                }
                else
                {
                    res.ParamObjectValue = null;
                }
                
                de.Key = _dt.Rows[_pos]["ParamName"].ToString();
                de.Value = res;
                return de;
            }
        }

        public object Key
        {
            get { return _dt.Rows[_pos]["ParamName"].ToString();}
        }

        public object Value
        {
            get { return Entry.Value; }
        }
        #endregion
    }
}
